home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 2.toast / pc / sample code / quicktime / capturing / hacktv.win / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-09-28  |  11.0 KB  |  441 lines

  1. /*
  2.     File:        Main.c
  3.  
  4.     Contains:    HackTV Windows application
  5.  
  6.     Written by: Brian Friedkin
  7.             
  8.     Copyright:    © 1992-1998 by Apple Computer, Inc.
  9. */
  10.  
  11. // Macintosh headers
  12. #include "QTML.h"
  13. #include "Movies.h"
  14. #include "Scrap.h"
  15. #include "Resources.h"
  16. #include "Common.h"
  17. #include "Globals.h"
  18.  
  19. // Windows headers
  20. #include <windows.h>
  21. #include "resource.h"
  22.  
  23. long FAR PASCAL FrameWndProc(HWND, UINT, UINT, LONG);
  24. long FAR PASCAL MonitorWndProc(HWND, UINT, UINT, LONG);
  25. int                DoIdleMenus(HWND hWnd, HMENU hMenu);
  26.  
  27. HANDLE    ghInst;
  28. HWND    ghWnd;
  29. HWND    ghWndMDIClient;
  30. char    szAppName[20];
  31. char    szChildName[] = "MdiChild";
  32. BOOL    gShuttingDown = FALSE;
  33.  
  34. /* ------------------------------------------------------------- */
  35.  
  36. int PASCAL WinMain (HANDLE hInstance, HANDLE hPrevInstance,
  37.                     LPSTR lpszCmdLine, int nCmdShow)
  38. {
  39.     HANDLE        hAccel ;
  40.     HWND        hwndFrame;
  41.     MSG            msg ;
  42.     char        szAppPathName[_MAX_PATH];
  43.     FSSpec        fsp;
  44.     short        appResID;
  45.     WNDCLASS    wc={0};
  46.  
  47.     ghInst = hInstance ;
  48.  
  49.     if (!hPrevInstance) 
  50.     {
  51.         LoadString(hInstance, IDS_APPNAME, szAppName, sizeof(szAppName));
  52.         
  53.         // Register the frame window class
  54.         wc.style         = CS_HREDRAW | CS_VREDRAW;
  55.         wc.lpfnWndProc   = (WNDPROC)FrameWndProc;
  56.         wc.cbClsExtra    = 0;
  57.         wc.cbWndExtra    = 0;
  58.         wc.hInstance     = hInstance;
  59.         wc.hIcon         = LoadIcon(0, IDI_APPLICATION);
  60.         wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  61.         wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
  62.         wc.lpszMenuName  = szAppName;
  63.         wc.lpszClassName = szAppName;
  64.         if (!RegisterClass(&wc))
  65.             return FALSE;
  66.  
  67.         // Register the Monitor child window class
  68.         wc.style         = 0;
  69.         wc.lpfnWndProc   = (WNDPROC)MonitorWndProc;
  70.         wc.cbClsExtra    = 0;
  71.         wc.cbWndExtra    = 0;
  72.         wc.hInstance     = hInstance;
  73.         wc.hIcon         = LoadIcon(0, IDI_APPLICATION);
  74.         wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  75.         wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
  76.         wc.lpszMenuName  = NULL;
  77.         wc.lpszClassName = szChildName;
  78.         if (!RegisterClass(&wc))
  79.             return FALSE;
  80.     }
  81.  
  82.     // Load accelerators
  83.     hAccel = LoadAccelerators(hInstance, szAppName);
  84.  
  85.     // Initialize QuickTime Media Layer
  86.     InitializeQTML(0);
  87.  
  88.     // Initialize QuickTime
  89.     EnterMovies();
  90.  
  91.     // Open up our resource file
  92.     GetModuleFileName(hInstance, szAppPathName, 256);
  93.     NativePathNameToFSSpec(szAppPathName, &fsp, 0);
  94.     appResID = FSpOpenResFile(&fsp, fsRdPerm);
  95.     UseResFile(appResID);
  96.  
  97.     // Create the main frame window
  98.     ghWnd = hwndFrame = CreateWindow (szAppName, szAppName,
  99.                                WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN,
  100.                                CW_USEDEFAULT, CW_USEDEFAULT,
  101.                                CW_USEDEFAULT, CW_USEDEFAULT,
  102.                                NULL, NULL, hInstance, NULL) ;
  103.  
  104.     // Show the window
  105.     ShowWindow(hwndFrame, nCmdShow);
  106.     UpdateWindow(hwndFrame);
  107.  
  108.     // Fire up the sequence grabber
  109.     InitializeSequenceGrabber();
  110.     SendMessage(ghWndMDIClient, WM_MDIACTIVATE, (WPARAM)GetPortNativeWindow(gMonitor), 0);  
  111.  
  112.     // Process messages
  113.     while (!gShuttingDown)
  114.     {
  115.         if (PeekMessage(&msg, 0, 0, 0, PM_NOREMOVE))
  116.         {
  117.             GetMessage(&msg, NULL, 0, 0);
  118.             {
  119.                 if (!TranslateMDISysAccel(ghWndMDIClient, &msg))
  120.                     if (!TranslateAccelerator(hwndFrame, hAccel, &msg))
  121.                     {
  122.                         TranslateMessage(&msg);
  123.                         DispatchMessage(&msg);
  124.                     }
  125.             }
  126.         }
  127.         else if (gSeqGrabber)
  128.             SGIdle(gSeqGrabber);
  129.     }
  130.  
  131.     // Clean up
  132.     if (gSeqGrabber != 0L)
  133.     {
  134.         CloseComponent(gSeqGrabber);
  135.         gSeqGrabber = 0L;
  136.     }    
  137.     
  138.     if (gMonitor != nil)
  139.         DestroyWindow(GetPortNativeWindow(gMonitor));
  140.     
  141.     // Set quit flag
  142.     ExitMovies();
  143.     TerminateQTML();
  144.  
  145.     return msg.wParam;
  146. }
  147.  
  148. /* ------------------------------------------------------------- */
  149.  
  150. long FAR PASCAL FrameWndProc (HWND hwnd, UINT message, UINT wParam, LONG lParam)
  151. {
  152.     HWND               hwndChild ;
  153.  
  154.     switch (message)
  155.     {
  156.         case WM_CREATE:          // Create the client window
  157.         {
  158.             CLIENTCREATESTRUCT ccs = {0};
  159.  
  160.             // Create the MDI client filling the client area
  161.             ghWndMDIClient = CreateWindow("mdiclient",
  162.                                          NULL,
  163.                                          WS_CHILD | WS_CLIPCHILDREN | WS_VSCROLL |
  164.                                          WS_HSCROLL | MDIS_ALLCHILDSTYLES,
  165.                                          0, 0, 0, 0,
  166.                                          hwnd,
  167.                                          (HMENU)0xCAC,
  168.                                          ghInst,
  169.                                          (LPVOID)&ccs);
  170.  
  171.             ShowWindow(ghWndMDIClient, SW_SHOW);
  172.         }
  173.         return 0;
  174.  
  175.         case WM_COMMAND:
  176.             switch (LOWORD(wParam))
  177.             {
  178.                 case IDM_EXIT:
  179.                     gShuttingDown = TRUE;
  180.                     SendMessage (hwnd, WM_CLOSE, 0, 0L) ;
  181.                     return 0 ;
  182.  
  183.                 case IDM_ABOUT:
  184.                     DoAboutDialog();
  185.                     return 0;
  186.  
  187.                 case IDM_PAGE_SETUP:
  188.                     DoPageSetup();
  189.                     break;
  190.  
  191.                 case IDM_PRINT:
  192.                     DoPrint();
  193.                     break;
  194.  
  195.                 case IDM_CUT:
  196.                 case IDM_COPY:
  197.                     DoCopyToClipboard();
  198.                     break;
  199.  
  200.                 case IDM_VIDEO_SETTINGS:
  201.                     DoVideoSettings();
  202.                     break;
  203.  
  204.                 case IDM_SOUND_SETTINGS:
  205.                     DoSoundSettings();
  206.                     break;
  207.  
  208.                 case IDM_RECORD_VIDEO:
  209.                     gRecordVideo = !gRecordVideo;
  210.                     break;
  211.  
  212.                 case IDM_RECORD_SOUND:
  213.                     gRecordSound = !gRecordSound;
  214.                     break;
  215.  
  216.                 case IDM_SPLIT_TRACKS:
  217.                     gSplitTracks = !gSplitTracks;
  218.                     break;
  219.  
  220.                 case IDM_QUARTER_SIZE:
  221.                     DoResize(4);
  222.                     break;
  223.  
  224.                 case IDM_HALF_SIZE:
  225.                     DoResize(2);
  226.                     break;
  227.  
  228.                 case IDM_FULL_SIZE:
  229.                     DoResize(1);
  230.                     break;
  231.  
  232.                 case IDM_RECORD:
  233.                     DoRecord();
  234.                     break;
  235.  
  236.                 default:            // Pass to active child
  237.                     hwndChild = (HWND)SendMessage (ghWndMDIClient, WM_MDIGETACTIVE, 0, 0L) ;
  238.                     if (IsWindow (ghWndMDIClient))
  239.                         SendMessage (hwndChild, WM_COMMAND, wParam, lParam) ;
  240.  
  241.                     break;        // and then to DefFrameProc
  242.             }
  243.             break;
  244.  
  245.         case WM_INITMENU:
  246.             if (GetMenu(hwnd) == (HMENU)wParam)
  247.                 return DoIdleMenus((HWND)SendMessage(ghWndMDIClient, WM_MDIGETACTIVE, 0, 0), (HMENU)wParam);
  248.             return 1;
  249.  
  250.         case WM_DESTROY :
  251.             gShuttingDown = TRUE;
  252.             PostQuitMessage(0);
  253.             return 0 ;
  254.     }
  255.  
  256.     return DefFrameProc(hwnd, ghWndMDIClient, message, wParam, lParam);
  257. }
  258.  
  259. /* ------------------------------------------------------------- */
  260.  
  261. // Create the playthru monitor window
  262. void CreateMonitorWindow()
  263. {
  264.     DWORD    dwVersion;
  265.     HWND    hWnd;
  266.  
  267.     // Create the monitor window
  268.     dwVersion = GetVersion();
  269.     if ((dwVersion < 0x80000000) || (LOBYTE(LOWORD(dwVersion)) < 4))
  270.     {
  271.         // This is Windows NT or Win32s, so use the WM_MDICREATE message
  272.         MDICREATESTRUCT mcs;
  273.  
  274.         mcs.szClass = szChildName;
  275.         mcs.szTitle = "Monitor"; 
  276.         mcs.hOwner  = ghInst;
  277.         mcs.x       = CW_USEDEFAULT;
  278.         mcs.y       = CW_USEDEFAULT;
  279.         mcs.cx      = 100;
  280.         mcs.cy      = 100;
  281.         mcs.style   = WS_CHILD | WS_CAPTION;
  282.         mcs.lParam  = 0;
  283.  
  284.         hWnd = (HWND) SendMessage(ghWndMDIClient,
  285.                                        WM_MDICREATE,
  286.                                        0,
  287.                                        (LPARAM)(LPMDICREATESTRUCT) &mcs);
  288.     }
  289.     else
  290.     {
  291.         // This method will only work with Windows 95, not Windows NT or Win32s
  292.         hWnd = CreateWindowEx(WS_EX_MDICHILD | WS_CHILD | WS_CAPTION,
  293.                                    szChildName,
  294.                                    "Monitor",
  295.                                    0,
  296.                                    CW_USEDEFAULT,
  297.                                    CW_USEDEFAULT,
  298.                                    100,
  299.                                    100,
  300.                                    ghWndMDIClient, 
  301.                                    NULL,
  302.                                    ghInst,
  303.                                    0);
  304.     }
  305.     if (hWnd)
  306.         gMonitor = GetNativeWindowPort(hWnd);
  307. }
  308.  
  309. /* ------------------------------------------------------------- */
  310.  
  311. // Destroy the monitor window
  312. void DestroyMonitorWindow(void)
  313. {
  314.     HWND    hWnd;
  315.  
  316.     if (hWnd = GetPortNativeWindow(gMonitor))
  317.     {
  318.         SendMessage(hWnd, WM_CLOSE, 0, 0);
  319.     }
  320.     gMonitor = 0;
  321. }
  322.  
  323. /* ------------------------------------------------------------- */
  324.  
  325. long FAR PASCAL MonitorWndProc (HWND hWnd, UINT message, UINT wParam, LONG lParam)
  326. {
  327.     switch(message)
  328.     {
  329.         case WM_CREATE:
  330.             // Associate a GrafPort with this window
  331.             CreatePortAssociation(hWnd, NULL, 0);
  332.             break;
  333.  
  334.         case WM_DESTROY:
  335.             // Destroy the port association
  336.             DestroyPortAssociation((CGrafPtr)gMonitor);
  337.             gMonitor = 0;
  338.             return 0;
  339.  
  340.         case WM_PAINT:
  341.             if (gSeqGrabber)
  342.             {
  343.                 PAINTSTRUCT    ps;
  344.                 HRGN    hRgnUpdate;
  345.  
  346.                 // Get the native update region
  347.                 if (hRgnUpdate = CreateRectRgn(0,0,0,0))
  348.                     GetUpdateRgn(hWnd, hRgnUpdate, 0);
  349.  
  350.                 UpdatePort(gMonitor);
  351.                 BeginPaint(hWnd, &ps);
  352.                 EndPaint(hWnd, &ps);
  353.                 if (hRgnUpdate)
  354.                 {
  355.                     RgnHandle    updateRgn;
  356.  
  357.                     if (updateRgn = NativeRegionToMacRegion(hRgnUpdate))
  358.                     {
  359.                         SGUpdate(gSeqGrabber, updateRgn);
  360.                         DisposeRgn(updateRgn);
  361.                     }
  362.                     DeleteObject(hRgnUpdate);
  363.                 }
  364.                 BeginUpdate(gMonitor);
  365.                 EndUpdate(gMonitor);
  366.             }
  367.             break;
  368.  
  369.         case WM_ENTERSIZEMOVE:
  370.             if (gSeqGrabber)
  371.                 SGPause(gSeqGrabber, true);
  372.             break;
  373.  
  374.         case WM_EXITSIZEMOVE:
  375.             // This is necessary, for now, to get the grab to start again afer the
  376.             // dialog goes away.  For some reason the video destRect never gets reset to point
  377.             // back to the monitor window.
  378.             if (gSeqGrabber)
  379.             {
  380.                 Rect    r;
  381.                 GrafPtr    oldPort, port = GetNativeWindowPort(hWnd);
  382.  
  383.                 GetPort(&oldPort);
  384.  
  385.                 // Make sure the port structures are up to date
  386.                 MacSetPort(port);
  387.                 UpdatePort(GetNativeWindowPort(hWnd));
  388.                 r = port->portRect;
  389.                 LocalToGlobal((Point*)&r.top);
  390.                 LocalToGlobal((Point*)&r.bottom);
  391.  
  392.                 // Call the grabber alignment proc.  This would have been called continuously
  393.                 // during DragAlignedWindow, but since we don't go though that API, we just
  394.                 // call it when the window is done moving.
  395.                 (*gSeqGrabberAlignProc.alignmentProc)(&r, gSeqGrabberAlignProc.alignmentRefCon);
  396.  
  397.                 MacSetPort(oldPort);
  398.  
  399.                 // Start 'er up
  400.                 SGPause(gSeqGrabber, false);
  401.             }
  402.             break;
  403.     }
  404.  
  405.     return DefMDIChildProc (hWnd, message, wParam, lParam);
  406. }
  407.  
  408. /* ------------------------------------------------------------- */
  409.  
  410. int DoIdleMenus(HWND hWnd, HMENU hMenu)
  411. {
  412.     // File menu
  413.     EnableMenuItem(hMenu, IDM_PRINT, (gVideoChannel != 0L ? MF_ENABLED : MF_GRAYED));
  414.  
  415.     // Edit menu
  416.     EnableMenuItem(hMenu, IDM_UNDO, MF_GRAYED);
  417.     EnableMenuItem(hMenu, IDM_CUT, (gVideoChannel != 0L) ? MF_ENABLED : MF_GRAYED);
  418.     EnableMenuItem(hMenu, IDM_COPY, (gVideoChannel != 0L) ? MF_ENABLED : MF_GRAYED);
  419.     EnableMenuItem(hMenu, IDM_PASTE, MF_GRAYED);
  420.     EnableMenuItem(hMenu, IDM_DELETE, MF_GRAYED);
  421.     
  422.     // Monitor menu
  423.     EnableMenuItem(hMenu, IDM_VIDEO_SETTINGS, (gVideoChannel != 0L) ? MF_ENABLED : MF_GRAYED);
  424.     EnableMenuItem(hMenu, IDM_SOUND_SETTINGS, (gSoundChannel != 0L) ? MF_ENABLED : MF_GRAYED);
  425.     EnableMenuItem(hMenu, IDM_RECORD_VIDEO, (gVideoChannel != 0L) ? MF_ENABLED : MF_GRAYED);
  426.     CheckMenuItem(hMenu, IDM_RECORD_VIDEO, (gVideoChannel && gRecordVideo) ? MF_CHECKED : MF_UNCHECKED); 
  427.     EnableMenuItem(hMenu, IDM_RECORD_SOUND, (gSoundChannel != 0L) ? MF_ENABLED : MF_GRAYED);
  428.     CheckMenuItem(hMenu, IDM_RECORD_SOUND, (gSoundChannel && gRecordSound) ? MF_CHECKED : MF_UNCHECKED); 
  429.     EnableMenuItem(hMenu, IDM_SPLIT_TRACKS, (gSoundChannel && gRecordSound && gVideoChannel && gRecordVideo) ? MF_ENABLED : MF_GRAYED);
  430.     CheckMenuItem(hMenu, IDM_SPLIT_TRACKS, gSplitTracks ? MF_CHECKED : MF_UNCHECKED); 
  431.     EnableMenuItem(hMenu, IDM_HALF_SIZE, (gVideoChannel != 0L) ? MF_ENABLED : MF_GRAYED);
  432.     CheckMenuItem(hMenu, IDM_HALF_SIZE, gHalfSize ? MF_CHECKED : MF_UNCHECKED); 
  433.     EnableMenuItem(hMenu, IDM_FULL_SIZE, (gVideoChannel != 0L) ? MF_ENABLED : MF_GRAYED);
  434.     CheckMenuItem(hMenu, IDM_FULL_SIZE, gFullSize ? MF_CHECKED : MF_UNCHECKED); 
  435.     EnableMenuItem(hMenu, IDM_RECORD, ((gSoundChannel && gRecordSound) || (gVideoChannel && gRecordVideo)) ? MF_ENABLED : MF_GRAYED);
  436.  
  437.     return 0;
  438. }
  439.  
  440. /* ------------------------------------------------------------- */
  441.